package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.web.service.base.StandardService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	// 模型驱动
	private Standard standard = new Standard();
	// 注入standardservice
	@Autowired
	public StandardService standardService;

	@Override
	public Standard getModel() {

		return standard;
	}
//添加按钮的action
	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "/pages/base/standard.html") })
	public String save() {
		System.out.println("添加收派标准....");
		standardService.save(standard);
		return SUCCESS;
	}
	//收派标准分页查询
	//属性驱动接受page rows
	private int page;
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Action(value="standard_pageQuery", results={@Result(name="result",type="json")})
	
	public String pageQuery(){
		//查询总记录数
	
		//查询当前页面数据
		Pageable pageAble =new PageRequest(page-1,rows);
		Page<Standard> pageData = standardService.findPageData(pageAble);
		//返回客户端需要的数据
		Map<String,Object> result=new HashMap<String,Object>() ;
		result.put("total", pageData.getNumberOfElements());
		result.put("rows",pageData.getContent());
		//将结果result转化成json返回通过是trust2
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
